package zouran.controller;

import org.springframework.web.bind.annotation.*;
import zouran.entity.Estimate;
import zouran.exception.MyRunTimeException;
import zouran.result.ErrorMessage;
import zouran.result.ResultMap;
import zouran.service.EarthquakeInfoService;
import zouran.service.EstimateService;

import java.util.Map;

/**
 * Estimate Api
 * Author：zouran
 * Date：2023/3/31  21:47
 * Description:
 */
@RestController
@RequestMapping("/estimate")
public class EstimateController {
    private final EstimateService estimateService;
    private final EarthquakeInfoService earthquakeInfoService;
    private final ResultMap<Estimate> resultMap;

    public EstimateController(EstimateService estimateService, EarthquakeInfoService earthquakeInfoService, ResultMap<Estimate> resultMap) {
        this.estimateService = estimateService;
        this.earthquakeInfoService = earthquakeInfoService;
        this.resultMap = resultMap;
    }


    /**
     * 地震id查询评估信息
     *
     * @param earthquakeId 地震编号
     * @return 该地震评估结果
     */
    @RequestMapping("/getEstimateResultByEarthquakeId")
    public ResultMap getEstimateResultByEarthquakeId(@RequestParam("earthquakeId") Long earthquakeId) {
        if (!earthquakeInfoService.judgeEarthquakeInfoExistById(earthquakeId))
            throw new MyRunTimeException(ErrorMessage.EARTHQUAKE_ID_ERROR.getMessage());
        return resultMap.success(estimateService.getEstimateResultByEarthquakeId(earthquakeId));
    }


    /**
     * 地震附近点烈度大小
     *
     * @param earthquakeId 地震编号
     * @param longitude    经度
     * @param latitude     纬度
     * @return 烈度数据
     */
    @RequestMapping("/getPointIntensity")
    public ResultMap getPointIntensity(@RequestParam("earthquakeId") Long earthquakeId, @RequestParam("longitude") Double longitude, @RequestParam("latitude") Double latitude) {
        if (!earthquakeInfoService.judgeEarthquakeInfoExistById(earthquakeId))
            throw new MyRunTimeException(ErrorMessage.EARTHQUAKE_ID_ERROR.getMessage());
        return resultMap.success(estimateService.getPointIntensity(earthquakeId, longitude, latitude));
    }

    @PostMapping("/getPoputionScope")
    public ResultMap getPoputionScope(@RequestBody Map<String,Double> scope){

        return resultMap.success(estimateService.getPopulationScope(scope));
    }


}
